package com.cheer.ba.view.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.footview.CustomerFooter;
import com.andview.refreshview.smileyloadingview.SmileyHeaderView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cheer.ba.R;
import com.cheer.ba.app.oss.OssManager;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.CommentAdp;
import com.cheer.ba.model.CommentModel;
import com.cheer.ba.presenter.impl.CommentPresenter;
import com.cheer.ba.utils.DensityUtils;
import com.cheer.ba.utils.ImageUtils;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.adapter.ReplyAdp;
import com.cheer.ba.view.animation.CustomAnimation;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.widget.MultiImageView;
import com.cheer.ba.widget.ShowPopupView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 回复评论
 * Created by Hq on 2017/7/4.
 */
public class ReplyCommentAty extends BaseCommAty implements ShowPopupView.OnItemPopupClick {

    //回复框
    @BindView(R.id.id_comment_reply_rela)
    RelativeLayout mReplyRela;
    //评论
    @BindView(R.id.id_reply_hint_txt)
    TextView mTxtReplyHint;

    //评论者头像
    @BindView(R.id.id_comment_head_img)
    ImageView mHeadImg;
    //姓名
    @BindView(R.id.id_comment_name_txt)
    TextView mTxtName;
    //删除
    @BindView(R.id.id_comment_del_txt)
    TextView mTxtDel;
    //时间
    @BindView(R.id.id_comment_time_txt)
    TextView mTxtTime;
    //内容
    @BindView(R.id.id_comment_content_txt)
    TextView mTxtContext;
    //图片
    @BindView(R.id.id_comment_pic_list)
    MultiImageView mMulti;
    //回复
    @BindView(R.id.id_look_reply_linear)
    LinearLayout mLinearReply;

    @BindView(R.id.id_comment_xrefreshv)
    XRefreshView mXRefreshView;
    @BindView(R.id.id_comment_recyclev)
    RecyclerView mRecycleView;
    private CommentAdp mReplyAdp;
    //存储柴油信息
    private List<CommentModel> mReplys = new ArrayList<CommentModel>();

    //记录删除评论或者回复的下标
    private int mRecordItemPos;
    //记录删除的评论或者回复的id
    private String mRecordItemId;
    //记录回复对象的custid
    private CommentModel mRecordModel;

    private CommentModel mComment;

    //弹出头像框
    private ShowPopupView popupView;
    public static final String[] POPUP_ITEMS = {"删除"};
    public static final int[] POPUP_ITEMS_COLOR = {R.color.red};

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_reply_comment, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new CommentPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initView() {
        popupView = new ShowPopupView(aty);
        popupView.setItemColor(POPUP_ITEMS_COLOR);
        popupView.setOnItemPopupClick(this);


        setTitleText("回复列表");
        mComment = (CommentModel) getIntent().getSerializableExtra(IntentKey.General.KEY_MODEL);
        if (null == mComment) {
            dataStatus(2);
            return;
        }
        ImageUtils.showCircleImg(aty, Protect.setPic(mComment.custImg), R.drawable.ic_user_head, R.drawable.ic_user_head, mHeadImg);
        mTxtName.setText(mComment.custNname);
        mTxtDel.setVisibility(View.GONE);
        mTxtTime.setText(StringUtils.getDetailStr(mComment.releaseDate));
        mTxtContext.setText(mComment.context);
        mMulti.setVisibility(View.GONE);
        mLinearReply.setVisibility(View.GONE);

        mTxtReplyHint.setHint("回复 " + mComment.custNname + ":");
        int width = DensityUtils.getScreenW(aty);
        setXRefreshView(mXRefreshView, true, true, false, false, new SmileyHeaderView(aty), new CustomerFooter(aty));
        mRecycleView.setLayoutManager(new LinearLayoutManager(aty));
        mRecycleView.setHasFixedSize(true);
        mReplyAdp = new CommentAdp(R.layout.adp_comment, mReplys, width);
//        mReplyAdp.openLoadAnimation(new CustomAnimation());
        mReplyAdp.setNotDoAnimationCount(3);
        mReplyAdp.isFirstOnly(false);
        mRecycleView.setAdapter(mReplyAdp);
    }

    @Override
    public void initData() {
        loadData(!hasDataInPage(), false);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(final boolean isPullDown) {
                loadData(false, false);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                loadData(false, true);
            }
        });
        mReplyAdp.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CommentModel model = (CommentModel) adapter.getItem(position);
                mRecordItemPos = position;
                switch (view.getId()) {
                    case R.id.id_comment_del_txt:
                        //删除回复
                        mRecordItemPos = position;
                        mRecordItemId = model.uid;
                        popupView.showAtLocPop(mRootViewLL, POPUP_ITEMS);
                        break;
                    case R.id.id_click_reply_linear:
                        //回复
                        mRecordModel = model;
                        ((CommentPresenter) presenter).showCommentPop(mRootViewLL, "2", "回复 " + model.custNname + ":",false);
                        break;
                }
            }
        });
    }

    @Override
    public void OnItemClick(int position, String tag) {
        //删除评论或者回复
        ((CommentPresenter) presenter).delMyComment(true, mRecordItemId);
    }

    /**
     * 加载数据
     *
     * @param loadMore 是否加载更多
     */
    public void loadData(boolean isShowLoad, boolean loadMore) {
        ((CommentPresenter) presenter).getReplyList(loadMore, isShowLoad, mComment.uid);
        if (!loadMore) {
            stopRefresh(mXRefreshView, 2000);
        } else {
            stopLoadMore(mXRefreshView, 2000);
        }
    }

    @OnClick({R.id.id_comment_reply_rela})
    public void onClick(View mView) {
        switch (mView.getId()) {
            case R.id.id_comment_reply_rela:
                //回复
                mRecordModel = mComment;
                ((CommentPresenter) presenter).showCommentPop(mRootViewLL, "2", "回复 " + mComment.custNname + ":",false);
                break;
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.DEL_COMMENT_SUCCESS)) {
                //删除评论成功
                if (mReplys.size() == 1) {
                    mReplys.clear();
                } else {
                    mReplys.remove(mRecordItemPos);
                }
                mReplyAdp.notifyDataSetChanged();
                mReplys.remove(mRecordItemPos);
                mReplyAdp.notifyDataSetChanged();
            }
        } else if (TextUtils.equals(tag, "2")) {
            //回复
            String context = (String) t;
            ((CommentPresenter) presenter).userReply(true, context, "2", mRecordModel.custId, mRecordModel.custId, mRecordModel.uid);
        } else if (TextUtils.equals(tag, ConstantTag.REPLY_SUCCESS)) {
            //回复成功
            ToastUtil.showToast("回复成功");
            CommentModel model = (CommentModel) t;
            if (null != model) {
                model.custNname = Session.getUserName();
                model.custImg = Session.getUserPicUrl().replace(OssManager.OSS_HOST, "");
                model.replyCustNname = mRecordModel.custNname;
                model.replyCustImg = mRecordModel.custImg;
                mReplys.add(0, model);
                mReplyAdp.notifyDataSetChanged();
            }
        }
    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.COMMENT_LIST_SUCCESS)) {
                if (!loadMore) {
                    mReplys.clear();
                    mXRefreshView.stopRefresh();
                } else {
                    mXRefreshView.stopLoadMore();
                }
                //柴油批发信息
                List<CommentModel> comments = (List<CommentModel>) t;
                if (!comments.isEmpty()) {
                    mReplys.addAll(comments);
                    mReplyAdp.notifyDataSetChanged();
                } else {
                    if (loadMore) {
//                        mXRefreshView.setLoadComplete(true);
                        ToastUtil.showToast(getString(R.string.no_more_data));
                    } else {
                        //没有数据
                    }
                }
            }
        }
    }
}
